package com.yanqu.road.server.cmd.house;

import com.yanqu.road.entity.config.player.HouseInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogHouseBankBar;
import com.yanqu.road.entity.player.UserHouseBar;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.house.HouseProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.HouseModule;
import com.yanqu.road.server.manger.config.HouseMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.Map;

@Cmd(code = Protocol.S_HOUSE_BAR_BUY, desc = "购买钱庄柜台")
public class HouseBarBuyCmd extends AbstractCommand {

	private int pos;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		HouseProto.HouseBarBuyReqMsg reqMsg = HouseProto.HouseBarBuyReqMsg.parseFrom(packet.getBytes());
		pos = reqMsg.getPos();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		int cost = HouseMgr.getHouseBarCost(pos);

		HouseInfo houseInfo = HouseMgr.getHouseInfo(player.getUserInfo().getHouseLv());

		if(houseInfo == null || houseInfo.getLevel() < GameConfig.HOUSE_BANK_UNLOCK_HOUSE_LEVEL){
			ret = GameErrorCode.E_HOUSE_BANK_LOCK;
			return;
		}

		Map<Integer, UserHouseBar> userHouseBarMap = player.getModule(HouseModule.class).getUserHouseBarMap();

		if((userHouseBarMap.size() != 0 && !userHouseBarMap.containsKey(pos - 1)) || (userHouseBarMap.size() == 0 && pos != 1)){
			ret = GameErrorCode.E_HOUSE_BAR_BUY_ORDER_BY;
			return;
		}

		if(userHouseBarMap.containsKey(pos)){
			ret = GameErrorCode.E_HOUSE_BAR_HAS_BUY;
			return;
		}

		if(userHouseBarMap.size() >= houseInfo.getBankBarNum()){
			ret = GameErrorCode.E_HOUSE_BAR_NUM_LIMIT;
			return;
		}

		Property costProperty = PropertyHelper.parseStringToProperty(GameConfig.GAME_MONEY_INGOTS + "=" + cost);
		if(!player.getModule(CurrencyModule.class).removeCurrency(costProperty,eLogMoneyType.HouseBank,eLogMoneyType.HouseBankBuyBar)){
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}

		player.getModule(HouseModule.class).addHouseBar(pos);
		player.getModule(HouseModule.class).syncUserHouseBank();

		AutoLogMgr.add(new LogHouseBankBar(
				player.getUserId(),
				player.getUserInfo().getEarnSpeed().toString(),
				player.getUserInfo().getTitleId(),
				player.getUserInfo().getVipExp(),
				pos,
				PropertyHelper.parsePropertyToString(costProperty),
				userHouseBarMap.size()
		));
	}

	@Override
	public void sendData(GamePlayer player) {
		HouseProto.HouseBarBuyRespMsg.Builder respMsg = HouseProto.HouseBarBuyRespMsg.newBuilder();
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_HOUSE_BAR_BUY, respMsg);
	}
}
